#include "headfiles.h"

using namespace std;

int main(int argc, char **argv) {
    if (argc != 3) {
        cout << "Usage: " << argv[0] << " arrayfile1 arrayfile2" << endl;
        return 0;
    }

    fstream fs;
    fs.open(argv[1], iostream::in);
    if (!fs.is_open()) {
        cout << "Arrayfile1 open error!" << endl;
        return 1;
    }
    vector<int> tmp;
    string str;
    while (fs >> str) {
        if (str.empty()) {
            break;
        }

        int val = atoi(str.c_str());
        tmp.push_back(val);
    }
    fs.close();

    int n1 = tmp.size();
    int *array1 = new int[n1];
    if (array1 == NULL) {
        cout << "Allocate array1 error!" << endl;
        return 1;
    }

    int i;
    for (i = 0; i < n1; i++) {
        array1[i] = tmp[i];
    }

    fs.open(argv[2], iostream::in);
    if (!fs.is_open()) {
        cout << "Arrayfile2 open error!" << endl;
        return 1;
    }
    tmp.clear();
    while (fs >> str) {
        if (str.empty()) {
            break;
        }

        int val = atoi(str.c_str());
        tmp.push_back(val);
    }
    fs.close();

    int n2 = tmp.size();
    int *array2 = new int[n2];
    if (array2 == NULL) {
        cout << "Allocate array2 error!" << endl;
        return 1;
    }

    for (i = 0; i < n2; i++) {
        array2[i] = tmp[i];
    }
    tmp.clear();

    double median = 0.0;
    median = twoArrayMedianDouble(array1, n1, array2, n2);

    cout << "Array1:" << endl;
    for (i = 0; i < n1; i++) {
        cout << array1[i] << "\t";
    }
    cout << endl << endl;

    cout << "Array2:" << endl;
    for (i = 0; i < n2; i++) {
        cout << array2[i] << "\t";
    }
    cout << endl << endl;

    cout << "Median = " << median << endl;

    delete array1;
    delete array2;
}

